package com.uyayo.framework.message.model;

import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 公共返回结果包装类
 * @author hfj
 *
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 3328525301170465491L;

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    //是否成功
    private boolean success;

    // 编码
    private Integer code;

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object data;
    
    // 跳转地址
    private String url;
    
	
	public Result() {
    }

    private Result(boolean success) {
    	this.success = success;
    }
    
    private Result(boolean success, T data) {
		super();
		this.success = success;
		this.data = data;
	}

    public Result(Integer status, String message) {
        this.status = status;
        this.message = message;
    }
    
    public Result(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    
    public Result(boolean success, Integer status, String message,
			T data) {
		super();
		this.success = success;
		this.status = status;
		this.message = message;
		this.data = data;
	}
    


    public static Result success() {
        return new Result(true);
    }
    
    public static Result success(Object data) {
        return new Result(true,data);
    }
    
    /**
     * 成功并附带消息
     * @param msg
     * @return
     */
    public static Result successAndMsg(String msg) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setMessage(msg);
        return result;
    }
    
    /**
     * 成功并重定向
     * @param url
     * @return
     */
    public static Result successAndRedirect(String url) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setUrl(url);
        return result;
    }
    
    public static Result success(Integer status) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setStatus(status);
        return result;
    }
    
    public static Result success(Integer status,String msg) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setStatus(status);
    	result.setMessage(msg);
        return result;
    }

    public static Result success(String msg,String url) {
        Result result = new Result();
        result.setSuccess(true);
        result.setMessage(msg);
        result.setUrl(url);
        return result;
    }

    public static Result success(Integer status, String msg, Object data) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setStatus(status);
    	result.setMessage(msg);
    	result.setData(data);
        return result;
    }
    

    
    
    
    
    public static Result error() {
        return new Result(false);
    }
    
    public static Result error(Object data) {
        return new Result(false,data);
    }
    
    /**
     * 失败并附带消息
     * @param msg
     * @return
     */
    public static Result errorAndMsg(String msg) {
    	Result result = new Result();
    	result.setSuccess(false);
    	result.setMessage(msg);
        return result;
    }
    
    /**
     * 失败并重定向
     * @param url
     * @return
     */
    public static Result errorAndRedirect(String url) {
    	Result result = new Result();
    	result.setSuccess(false);
    	result.setUrl(url);
        return result;
    }
    
    public static Result error(Integer status) {
    	Result result = new Result();
    	result.setSuccess(false);
    	result.setStatus(status);
        return result;
    }
    
    public static Result error(Integer status,String msg) {
    	Result result = new Result();
    	result.setSuccess(false);
    	result.setStatus(status);
    	result.setMessage(msg);
        return result;
    }
    
    
    public static Result error(Integer status,String msg, Object data) {
    	Result result = new Result<>();
    	result.setSuccess(false);
    	result.setStatus(status);
    	result.setMessage(msg);
    	result.setData(data);
        return result;
    }
    
    public static Result build(Integer status, String msg, Object data) {
        return new Result(status, msg, data);
    }
    
	public static Result build(Integer status, String msg) {
        return new Result(status, msg, null);
    }
    

    
    
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Integer getStatus() {
        return status;
    }

	public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    
    public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
     * 将json结果集转化为TaotaoResult对象
     * 
     * @param jsonData json数据
     * @param clazz TaotaoResult中的object类型
     * @return
     */
    public static Result formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, Result.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     * 
     * @param json
     * @return
     */
    public static Result format(String json) {
        try {
            return MAPPER.readValue(json, Result.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     * 
     * @param jsonData json数据
     * @param clazz 集合中的类型
     * @return
     */
    public static Result formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }
}
